<?php
/**
 * Created by IntelliJ IDEA.
 * User: m1573
 * Date: 2022/7/14
 * Time: 19:48
 */
    include("include/util.inc.php");


    function loadTableQuestion($PDO){
        $path = "database.csv";
        $file_arr = file($path);
        foreach($file_arr as $line){
            $arr = explode('#',$line);
            $sql = "INSERT INTO 
                  question(category,level_part,question_part,answer_part) 
                  VALUE(?,?,?,?)";
            $sth = $PDO ->prepare($sql);
            $sth->bindParam(1,$arr[0]);
            $sth->bindParam(2,$arr[1]);
            $sth->bindParam(3,$arr[2]);
            $sth->bindParam(4,$arr[3]);

            $sth->execute();
        }
    }
    function loadTableCategory($PDO){
        $path = "database.csv";
        $file_arr = file($path);
        $categorys = array();
        foreach ($file_arr as $line){
            $category = explode('#',$line)[0];
            $categorys[$category] = $category;
        }

        foreach($categorys as $category){
            $sql = "INSERT INTO 
                      category(cname) 
                      VALUE(?)";
            $sth = $PDO ->prepare($sql);
            $sth->bindParam(1,$category);
            $sth->execute();
        }
    }

    function loadData2MySql($PDO){
        loadTableQuestion($PDO);
        loadTableCategory($PDO);
    }


    loadData2MySql($PDO);

?>